﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OMDRun
{
    public class DelegateAction
    {

        delegate void Delegate_Enable(Control control, bool b);
        /// <summary>
        ///  委托设置
        /// </summary>
        public static void SetEnable(Control control, bool b)
        {
            if (control.InvokeRequired)
            {
                Delegate_Enable delegate_Enable = new Delegate_Enable(SetEnable);
                control.Invoke(delegate_Enable, new object[] { control, b });
            }
            else
            {
                control.Enabled = b;

            }
        }



        delegate void Delegate_BindingListRemove<T>(DataGridView dataGridView, BindingList<T> bindingList, T t);


        public static void BindingListRemove<T>(DataGridView dataGridView, BindingList<T> bindingList, T t)
        {
            if (dataGridView.InvokeRequired)
            {
                Delegate_BindingListRemove<T> delegate_BindingListRemove = new Delegate_BindingListRemove<T>(BindingListRemove);
                dataGridView.Invoke(delegate_BindingListRemove, new object[] { dataGridView, bindingList, t });
            }
            else
            {
                bindingList.Remove(t);

            }
        }


        delegate void Delegate_BindingListAdd<T>(DataGridView dataGridView, BindingList<T> bindingList, T t);


        public static void BindingListAdd<T>(DataGridView dataGridView, BindingList<T> bindingList, T t)
        {
            if (dataGridView.InvokeRequired)
            {
                Delegate_BindingListAdd<T> delegate_BindingListAdd = new Delegate_BindingListAdd<T>(BindingListAdd);
                dataGridView.Invoke(delegate_BindingListAdd, new object[] { dataGridView, bindingList, t });
            }
            else
            {
                bindingList.Add(t);

            }
        }



        delegate void Delegate_BindingList<T>(DataGridView dataGridView, List<T> list);
        public static void BindingList<T>(DataGridView dataGridView, List<T> list)
        {
            if (dataGridView.InvokeRequired)
            {
                Delegate_BindingList<T> delegate_BindingList = new Delegate_BindingList<T>(BindingList);
                dataGridView.Invoke(delegate_BindingList, new object[] { dataGridView, list });
            }
            else
            {
                dataGridView.DataSource = list;

            }
        }




        delegate void Delegate_TxtAutoComplete(TextBox textBox, AutoCompleteStringCollection source, List<string> list);
        public static void TxtAutoCompleteBinding(TextBox textBox, AutoCompleteStringCollection source, List<string> list)
        {
            if (textBox.InvokeRequired)
            {
                Delegate_TxtAutoComplete delegate_TxtAutoComplete = new Delegate_TxtAutoComplete(TxtAutoCompleteBinding);
                textBox.Invoke(delegate_TxtAutoComplete, new object[] { textBox, source, list });
            }
            else
            {
                source.Clear();
                source.AddRange(list.ToArray());

            }
        }


        public delegate void mydelegate();
        public static void DoInvoke(Form form, mydelegate method)
        {
            form.BeginInvoke(new MethodInvoker(delegate
            {
                method();
            }));

        }
    }
}